<?php
namespace Sys\Controller;
use Common\Controller\BaseController;
class DirectorController extends BaseController{
	
    public function show(){
        if($_SESSION['tp']=='1') {
            if($ex=M('exams')->join('pj_exam_items ON pj_exams.type_dict_id = pj_exam_items.id')->where(array('student_id'=>$_SESSION['id']))->select()){

            }else{
                $ex='暂无考试成绩';
            }
            if($po=M('points')->where(array('student_id'=>$_SESSION['id']))->select()){
                foreach($po as $k=>$v){
                      $pri=M('points_rule')->field('rule_name')->select($v['point_rule_id']);
                    $pris='';
                   foreach($pri as $j){
                       $pris.=$j['rule_name'].',';
                   }
                   $pris=substr($pris,0,-1);
                    $po[$k]['rule_name']=$pris;
                }
            }else{
                $po='暂无扣分记录';
            }
            $this->assign('ex',$ex);
            $this->assign('po',$po);
            $this->display();
        }
        if($_SESSION['tp']=='0'){
			
            $class=M('teachers')->find($_SESSION['id']);
			
            $stuName=M('students')->where(array('term_id'=>$class['tclass']))->select();
			
            $this->assign('sn',$stuName);
            $this->display('showTeacher');
        }
    }
    public function getScore(){
        $id = I('post.id');
        $exaNum=0;
        $poiNum=0;
        if($poi = M('points')->where(array('student_id' => $id))->select()) {
            foreach ($poi as $k => $v) {
                $poiNum=$poiNum+$v['score'];
                 $pri = M('points_rule')->field('rule_name')->select($v['point_rule_id']);
                $pris = '';
                foreach ($pri as $j) {
                    $pris .= $j['rule_name'] . ',';
                }
                $pris = substr($pris, 0, -1);
                $poi[$k]['rule_name'] = $pris;
            }
        }
        $exa = M('exams')->where(array('student_id' => $id))
            ->join('pj_exam_items ON pj_exams.type_dict_id = pj_exam_items.id')
            ->join('pj_percent on pj_exams.type_dict_id=pj_percent.dict_id')
            ->select();
        if($exa){
            foreach($exa as $v){
                $exaNum=$exaNum+floatval($v['score']*$v['percent']/100);
            }

        }
        $res['poi'] = $poi ? $poi : '暂无扣分记录';
        $res['exa'] = $exa ? $exa : '暂无考试成绩记录';
        $res['num']=[
            'exaNum'=>$exaNum,
            'poiNum'=>$poiNum,
        ];
        $this->ajaxReturn($res);
    }
    public function filed(){
        $id = I('post.id');
        if($stu=M('students')->find($id)) {
            $exaNum = 0;
            $poiNum = 0;
            if ($poi = M('points')->where(array('student_id' => $id))->select()) {
                $pois = M('points')->where(array('student_id' => $id))
                    ->field('points_time,score')
                    ->select();
                foreach ($poi as $k => $v) {
                    $poiNum = $poiNum + $v['score'];
                    $pri = M('points_rule')->field('rule_name')->select($v['point_rule_id']);
                    $pris = '';
                    foreach ($pri as $j) {
                        $pris .= $j['rule_name'] . ',';
                    }
                    $pris = substr($pris, 0, -1);
                    $pois[$k]['rule_name'] = $pris;
                }
            }
             $exa = M('exams')->where(array('student_id' => $id))
                ->join('pj_exam_items ON pj_exams.type_dict_id = pj_exam_items.id')
                ->join('pj_percent ON pj_exams.type_dict_id = pj_percent.dict_id')
                ->select();
				
			$exas = M('exams')->where(array('student_id' => $id))
                ->field('exam_name,score,examtime')
                ->join('pj_exam_items ON pj_exams.type_dict_id = pj_exam_items.id')
                ->join('pj_percent ON pj_exams.type_dict_id = pj_percent.dict_id')
                ->select();
				
            if ($exa) {
                foreach ($exa as $v) {
                    $exaNum = $exaNum + floatval($v['score'] * $v['percent'] / 100);
                }

            }
            $fil['student'] = $stu;
            $fil['points'] = $pois ? $pois : '暂无扣分记录';
            $fil['exam'] = $exas ? $exas : '暂无考试成绩记录';
			//dump($fil['exam']);
            $fil['scoreall'] = $exaNum - $poiNum;
            $fileds=json_encode($fil);
            $tim=date('Y/m/d H:i:s',time());
            $data=[
                'content'=>$fileds,
                'ftime'=>$tim,
				'student_id'=>$id
            ];
            if(M('filed')->add($data)){
                $res='归档成功';
            }else{
                $res='归档失败';
            }
        }else{
            $res='无该学生的相关记录';
        }
        $this->ajaxReturn($res);
    }
	public function showFiled(){
		 $filed=M('filed')->field('content')->where(["student_id"=>I('get.id')])->find();
		 $content=json_decode($filed['content'],true);//print_r($content);
		 $this->assign('student',$content['student']);
		 $this->assign('points',$content['points']);
		 $this->assign('exam',$content['exam']);
		 //$this->assign('scoreall',$content['scoreall']);
		 $this->display();
}
	public function index(){
		$M = new \think\model;
		$ids = $M -> query('SELECT GROUP_CONCAT(student_id) as aa FROM (SELECT student_id FROM `pj_filed` GROUP BY student_id) a');
		$m = M('students')->field('student_id,sname')->select($ids[0]['aa']);
		$this->assign('student',$m);
		$this->display();
	}
}